# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_bloat:pw_cc_size_binary.bzl", "pw_cc_size_binary")
load("//pw_bloat:pw_size_diff.bzl", "pw_size_diff")
load("//pw_bloat:pw_size_table.bzl", "pw_size_table")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = ["-ctad_warnings"],
)

licenses(["notice"])

# Libraries

cc_library(
    name = "named_u32",
    testonly = True,
    hdrs = ["public/examples/named_u32.h"],
    strip_include_prefix = "public",
    deps = [
        "//pw_bytes",
        "//pw_string",
    ],
)

cc_library(
    name = "custom_allocator",
    srcs = ["custom_allocator.cc"],
    hdrs = ["public/examples/custom_allocator.h"],
    strip_include_prefix = "public",
    deps = [
        "//pw_allocator",
        "//pw_log",
        "//pw_result",
    ],
)

cc_library(
    name = "custom_allocator_test_harness",
    testonly = True,
    hdrs = ["public/examples/custom_allocator_test_harness.h"],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        ":custom_allocator",
        "//pw_allocator:test_harness",
        "//pw_allocator:testing",
    ],
)

# Examples

pw_cc_test(
    name = "basic",
    srcs = ["basic.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":named_u32",
        "//pw_allocator",
        "//pw_allocator:testing",
    ],
)

pw_cc_test(
    name = "block_allocator",
    srcs = ["block_allocator.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":named_u32",
        "//pw_allocator:first_fit",
    ],
)

pw_cc_test(
    name = "custom_allocator_perf_test",
    srcs = ["custom_allocator_perf_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":custom_allocator_test_harness",
        "//pw_perf_test",
        "//pw_perf_test:state",
        "//pw_random",
    ],
)

pw_cc_test(
    name = "custom_allocator_test",
    srcs = ["custom_allocator_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":custom_allocator",
        ":custom_allocator_test_harness",
        ":named_u32",
        "//pw_allocator:fuzzing",
        "//pw_allocator:testing",
        "//pw_containers:vector",
        "//pw_fuzzer:fuzztest",
    ],
)

pw_cc_test(
    name = "linker_sections",
    srcs = ["linker_sections.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":named_u32",
        "//pw_allocator",
        "//pw_allocator:first_fit",
        "//pw_allocator:worst_fit",
        "//pw_allocator/block:detailed_block",
    ],
)

pw_cc_test(
    name = "metrics",
    srcs = ["metrics.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":named_u32",
        "//pw_allocator:metrics",
        "//pw_allocator:testing",
        "//pw_allocator:tracking_allocator",
        "//pw_tokenizer",
    ],
)

pw_cc_test(
    name = "pmr",
    testonly = True,
    srcs = ["pmr.cc"],
    features = ["-conversion_warnings"],
    deps = [
        "//pw_allocator",
        "//pw_allocator:pmr_allocator",
        "//pw_allocator:testing",
    ],
)

# DOCSTAG: [pw_allocator-examples-size_report-bazel]
pw_cc_size_binary(
    name = "size_report",
    srcs = ["size_report.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":custom_allocator",
        "//pw_allocator:best_fit",
        "//pw_allocator/size_report",
        "//pw_bloat:bloat_this_binary",
    ],
)

pw_size_diff(
    name = "custom_allocator_size_diff",
    base = "//pw_allocator/size_report:best_fit",
    label = "CustomAllocator",
    target = ":size_report",
)

pw_size_table(
    name = "custom_allocator_size_report",
    reports = [":custom_allocator_size_diff"],
)
# DOCSTAG: [pw_allocator-examples-size_report-bazel]

pw_cc_test(
    name = "spin_lock",
    srcs = ["spin_lock.cc"],
    features = ["-conversion_warnings"],
    # TODO: b/358411629 - This test times out on rp2.
    target_compatible_with = select({
        "@pico-sdk//bazel/constraint:rp2040": ["@platforms//:incompatible"],
        "@pico-sdk//bazel/constraint:rp2350": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    deps = [
        ":named_u32",
        "//pw_allocator:synchronized_allocator",
        "//pw_allocator:testing",
        "//pw_assert:check",
        "//pw_sync:interrupt_spin_lock",
        "//pw_thread:test_thread_context",
        "//pw_thread:thread",
        "//pw_thread:thread_core",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "BUILD.bazel",
        "basic.cc",
        "block_allocator.cc",
        "custom_allocator.cc",
        "custom_allocator_perf_test.cc",
        "custom_allocator_test.cc",
        "linker_sections.cc",
        "metrics.cc",
        "pmr.cc",
        "public/examples/custom_allocator.h",
        "public/examples/custom_allocator_test_harness.h",
        "size_report.cc",
        "spin_lock.cc",
        ":custom_allocator_size_report",
    ],
    target_compatible_with = incompatible_with_mcu(),
)
